const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;


const UserSchema = new Schema({
    username: {
        type: String,
        required: true,
        maxlength: 16,
        minlength: 3
    },
    password: {
        type: ObjectId,
        ref: 'Password'
    },
    email: {
        type: String,
        required: true,
        unique: true,
        validate: {
            validator: (v) => /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/.test(v)
        }
    },
    role: Number,
    state: Number
})

const User = mongoose.model('User', UserSchema)

module.exports = {
    User
}

// const us = new User({
//     username: 'admin',
//     password: '123456',
//     email: '123@123.com',
//     role: 1,
//     state: 1
// })
// us.save();